package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TStuff;
import hl.model.TVenueInfo;
import hl.pageModel.DataGrid;
import hl.pageModel.Stuff;
import hl.service.StuffService;

@Service("stuffService")
public class StuffServiceImpl implements StuffService {

	private BaseDao<TStuff> stuffDao;

	private BaseDao<TVenueInfo> venueInfoDao;

	public BaseDao<TVenueInfo> getVenueInfoDao() {
		return venueInfoDao;
	}

	@Autowired
	public void setVenueInfoDao(BaseDao<TVenueInfo> venueInfoDao) {
		this.venueInfoDao = venueInfoDao;
	}

	public BaseDao<TStuff> getStuffDao() {
		return stuffDao;
	}

	@Autowired
	public void setStuffDao(BaseDao<TStuff> stuffDao) {
		this.stuffDao = stuffDao;
	}

	@Override
	public List getNotRegisterStuff() {
		List<TStuff> lts = stuffDao.getList(TStuff.class,
				"select t.* from t_stuff t left join t_pc_operator p on t.PK_STUFF=p.PK_STUFF where p.PK_STUFF is null");
		List<Stuff> lt = new ArrayList();
		if (lts != null && lts.size() > 0) {
			for (TStuff t : lts) {
				Stuff u = new Stuff();
				u.setId(t.getPkStuff().toString());
				u.setName(t.getName());
				lt.add(u);
			}
		}
		return lt;
	}

	@Override
	public DataGrid getDatagrid(Stuff stuff) {
		DataGrid dg = new DataGrid();
		String hql = "from TStuff t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(stuff, hql, params);

		if (stuff.getOrder() != null) {
			hql += " order by " + stuff.getSort() + " " + stuff.getOrder();
		}

		List<TStuff> l = stuffDao.find(hql, params, stuff.getPage(), stuff.getRows());

		int totalHql = stuffDao.count(hql, params);

		List<Stuff> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TStuff t : l) {
				Stuff u = new Stuff();
				BeanUtils.copyProperties(t, u);
				u.setVenueName(t.getTVenueInfo().getName());
				u.setPkVenueInfo(t.getTVenueInfo().getPkVenueInfo());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(Stuff stuff, String hql, Map<String, Object> params) {
		if (stuff.getName() != null && !stuff.getName().trim().equals("")) {
			hql += " where t.name like :value1 ";
			params.put("value1", "%" + stuff.getName().trim() + "%");
		}

		if (stuff.getGender() != null && !stuff.getGender().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.gender like :value2 ";
			params.put("value2", "%" + stuff.getGender().trim() + "%");
		}
		if (stuff.getPkStuff() != null && !stuff.getPkStuff().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.pkStuff like :value3 ";
			params.put("value3", "%" + stuff.getPkStuff().toString() + "%");
		}
		return hql;
	}

	@Override
	public void save(Stuff stuff) {
		TStuff t = new TStuff();
		BeanUtils.copyProperties(stuff, t);
		t.setCdatetime(new Date());
		t.setTVenueInfo(venueInfoDao.get(TVenueInfo.class, stuff.getPkVenueInfo()));
		stuffDao.save(t);
	}

	@Override
	public void edit(Stuff stuff) {
		TStuff t = new TStuff();
		BeanUtils.copyProperties(stuff, t);
		t.setMdatetime(new Date());
		t.setTVenueInfo(venueInfoDao.get(TVenueInfo.class, stuff.getPkVenueInfo()));
		stuffDao.saveOrUpdate(t);
	}

	@Override
	public void remove(Stuff stuff) {
		TStuff t = stuffDao.get(TStuff.class, stuff.getPkStuff());
		stuffDao.delete(t);
	}

	@Override
	public Object getComboBox() {
		List<TStuff> l = stuffDao.find("from TStuff");
		List<Stuff> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TStuff t : l) {
				Stuff u = new Stuff();
				BeanUtils.copyProperties(t, u);
				u.setVenueName(t.getTVenueInfo().getName());
				u.setPkVenueInfo(t.getTVenueInfo().getPkVenueInfo());
				pl.add(u);
			}
		}
		return pl;
	}

}
